/**
 * 
 */
package com.dingjian.meal.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dingjian.base.CoreEntity;
import com.dingjian.base.service.BaseService;
import com.dingjian.base.util.StringUtils;
import com.dingjian.framework.base.controller.ListController;
import com.dingjian.meal.model.Dishes;
import com.dingjian.meal.model.Restaurant;
import com.dingjian.meal.service.RestaurantService;

/**
 *
 * @author FengMy
 * @since 2012-6-8
 */
@Controller
@RequestMapping(value="meal/restaurant/*")
public class RestaurantListController extends ListController {
	@Resource(name="restaurantService")
	private RestaurantService restaurantService;
	@RequestMapping(value="list")
	public String list(){
		return "meal/restaurantList";
	}
	
	@RequestMapping(value="enable")
	public void enable(HttpServletResponse response){
		String id = getString("id");
		boolean enable = "1".equals(getString("enable"));
		restaurantService.enable(id, enable);
		outPrint(response, 1);
	}
	
	@Override
	public String add(ModelMap model) {
		CoreEntity entity = createNewEntity();
		model.put("data", entity);
		model.put("entryData", JSONObject.fromObject(entity).toString());
		return getEditView();
	}
	
	@Override
	public String edit(ModelMap model, String id) {
		Restaurant restaurant = restaurantService.getEntityById(id);
		model.put("entryData", JSONObject.fromObject(restaurant).toString());
		model.put("data", restaurant);
		return getEditView();
	}
	
	@Override
	protected CoreEntity createNewEntity() {
		Restaurant restaurant = new Restaurant();
		restaurant.setDishes(new ArrayList<Dishes>());
		return restaurant;
	}
	@Override
	protected String getListView() {
		return "meal/restaurantList";
	}
	@Override
	protected String getEditView() {
		return "meal/restaurantEdit";
	}
	@Override
	protected String getListMapper() {
		return "com.dingjian.meal.dao.RestaurantDao.select";
	}

	@Override
	protected BaseService<Restaurant> getService() {
		return restaurantService;
	}
}
